package com.pccw.hessian.support.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;

import com.caucho.hessian.client.HessianHttpProxyFactory;
import com.caucho.hessian.io.HessianRemoteObject;
import com.pccw.hessian.support.cache.CacheHandler;
import com.pccw.hessian.support.client.HessianCAHProxy.RemoteProxy;

/**
 * Hessian cache,asynchronous,http ProxyFactory
 * @author guxuede
 *
 */
public class HessianCAHProxyFactory extends HessianHttpProxyFactory{
	private HessianAsynTaskExecuter asynTaskExecuter;
	
    @SuppressWarnings({"unchecked"})
    public <T>T create(Class<T> api, String urlName, CacheHandler cacheHandler,HessianAsynTaskExecuter executer,ExceptionHandler exceptionHandler,RemoteProxy remoteURL) throws MalformedURLException {
        if (api == null) {
            throw new NullPointerException("api must not be null for HessianProxyFactory.create()");
        }
        if(executer==null){
        	 throw new NullPointerException("AsynTaskExecuter must not be null");
        }
        if(exceptionHandler==null){
       	 throw new NullPointerException("ExceptionHandler must not be null");
       }
        asynTaskExecuter=executer;
        InvocationHandler handler;
        URL url = new URL(urlName);
        handler = new HessianCAHProxy(this, url,cacheHandler,exceptionHandler,remoteURL);
        return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{api, HessianRemoteObject.class}, handler);
    }

	public HessianAsynTaskExecuter getAsynTaskExecuter() {
		return asynTaskExecuter;
	}

	public void setAsynTaskExecuter(HessianAsynTaskExecuter asynTaskExecuter) {
		this.asynTaskExecuter = asynTaskExecuter;
	}

    
}
